package ar.com.ford.it.fullEconomics.view.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.MappingDispatchAction;
import ar.com.ford.it.fullEconomics.facade.UserFacade;
import ar.com.ford.it.fullEconomics.persistence.dto.Tms0008UsrprofVw;
import ar.com.ford.it.fullEconomics.security.FullEconomicsSessionControllerDummy;
import ar.com.ford.it.fullEconomics.view.form.LoginForm;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.websession.WebSessionHelper;

public class LoginAction extends MappingDispatchAction {
	
    public ActionForward showLogin(ActionMapping mapping, ActionForm actionForm, 
			   HttpServletRequest req, HttpServletResponse res){
    	
    	LoginForm form = (LoginForm) actionForm;
    	form.resetForm();
        return mapping.findForward("success");
        
    }

	public ActionForward doLogin(ActionMapping mapping, ActionForm actionForm, 
			   HttpServletRequest request, HttpServletResponse response) {

		LoginForm form = (LoginForm) actionForm;
		UserFacade facade = (UserFacade) FacadeProvider.getFacade(request,
				UserFacade.class);

		ActionForward res = null;

		Tms0008UsrprofVw user = new Tms0008UsrprofVw();
		user.setMs0008UserCodeC(form.getUserID());
		user.setMs0008UserPassword(form.getPassword());

		if (facade.validateUser(user)) {
			FullEconomicsSessionControllerDummy sessionControler = new FullEconomicsSessionControllerDummy();
			
			WebSessionHelper helper = new WebSessionHelper();
	        helper.registerSession(request, form.getUserID());
			
//			sessionControler.registerUser(request, form.getUserID());
		
			res = mapping.findForward("success");				
		} else {
			ActionMessages am = new ActionMessages();
			am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("login.error"));
			saveErrors(request, am);
			res = mapping.findForward("error");	
		}
		
		return res;
	}

}
